package cn.manter.basic.controller;

import cn.manter.basic.dto.SmsCodeBto;
import cn.manter.basic.exception.BusinessException;
import cn.manter.basic.service.IVerifyCodeService;
import cn.manter.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public AjaxResult graph(@PathVariable String key){
        try {
            String base64Str = verifyCodeService.graph(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody SmsCodeBto smsCodeBto){
        try {
            verifyCodeService.smsCode(smsCodeBto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
}
